home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / TELE.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  3KB  |  112 lines

  1. /**************************************************************************
  2.  *
  3.  * tele.cpp - telephone directory sample program, from section 9.3.1
  4.  *
  5.  * $Id: tele.cpp,v 1.4 1995/08/29 19:03:44 oberg Exp $
  6.  *
  7.  * $$RW_INSERT_HEADER "slyrs.cpp"
  8.  *
  9.  **************************************************************************/
  10.  
  11. # include <map>
  12. # include <algorithm>
  13. # include <vector>
  14. # include <iostream.h>
  15. # include <string>
  16. using namespace std;
  17.  
  18. typedef map<string, long, less<string> > friendMap;
  19. typedef map<long, string, less<long> >   sortedMap;
  20.  
  21. //
  22. //    utility functions used in telephone directory
  23. //
  24.  
  25. typedef friendMap::value_type entry_type;
  26. typedef sortedMap::value_type sorted_entry_type;
  27.  
  28. void printEntry(const entry_type & entry)
  29.     { cout << entry.first << ":" << entry.second << endl; }
  30.  
  31. void printSortedEntry(const sorted_entry_type & entry)
  32.     { cout << entry.first << ":" << entry.second << endl; }
  33.  
  34. int prefix(const entry_type& entry)
  35.     { return entry.second / 10000; }
  36.  
  37. bool prefixCompare(const entry_type & a, const entry_type & b)
  38.     { return prefix(a) < prefix(b); }
  39.     
  40. class checkPrefix {
  41. public:
  42.     checkPrefix (int p) : testPrefix(p) { }
  43.     int testPrefix;
  44.     bool operator () (const entry_type& entry)
  45.         { return prefix(entry) == testPrefix; }
  46. };
  47.             
  48. class telephoneDirectory {
  49. public:
  50.     void addEntry (string name, long number)
  51.         { database[name] = number; }
  52.         
  53.     void remove (string name)
  54.         { database.erase(name); }
  55.     
  56.     void update (string name, long number)
  57.         { remove(name); addEntry(name, number); }
  58.         
  59.     void displayDatabase()
  60.         { for_each(database.begin(), database.end(), printEntry); }
  61.     
  62.     void displayPrefix(int);
  63.     
  64.     void displayByPrefix();    
  65.     
  66. private:
  67.     friendMap database;
  68. };
  69.  
  70. void telephoneDirectory::displayPrefix(int prefix)
  71. {
  72.     cout << "Listing for prefix " << prefix << endl;
  73.     map<string, long, less<string> >::iterator where;
  74.     where = find_if(database.begin(), database.end(), checkPrefix(prefix));
  75.     while (where != database.end()) {
  76.         printEntry(*where);
  77.         where = find_if(++where, database.end(), checkPrefix(prefix));
  78.         }
  79.     cout << "end of prefix listing" << endl;
  80. }
  81.  
  82. void telephoneDirectory::displayByPrefix()
  83. {
  84.     cout << "Display by prefix" << endl;
  85.  
  86.     sortedMap sortedData;
  87.  
  88.     for (friendMap::iterator i = database.begin(); i != database.end(); i++)
  89.         sortedData.insert(sortedMap::value_type((*i).second,
  90.                             (*i).first));
  91.  
  92.     for_each(sortedData.begin(), sortedData.end(), printSortedEntry);
  93.     cout << "end display by prefix" << endl;
  94. }
  95.  
  96. int main() {
  97.     cout << "Telephone Directory sample program" << endl;
  98.     telephoneDirectory friends;
  99.     
  100.     friends.addEntry("Samantha", 6342343);
  101.     friends.addEntry("Brenda", 5436546);
  102.     friends.addEntry("Fred", 7435423);
  103.     friends.addEntry("Allen", 6348723);
  104.     
  105.     friends.displayDatabase();
  106.     friends.displayPrefix(634);
  107.     friends.displayByPrefix();
  108.     
  109.     cout << "End of telephone directory sample program" << endl;
  110.     return 0;
  111. }
  112.